package com.ksr.logistics.controller;

import com.github.pagehelper.PageInfo;
import com.ksr.logistics.pojo.BaseData;
import com.ksr.logistics.pojo.Customer;
import com.ksr.logistics.pojo.Result;
import com.ksr.logistics.pojo.User;
import com.ksr.logistics.service.BaseDataService;
import com.ksr.logistics.service.CustomerService;
import com.ksr.logistics.service.UserService;
import com.ksr.logistics.utilities.Constant;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private UserService userService;

    @Autowired
    private BaseDataService baseDataService;

    @RequestMapping("/list")
    @RequiresPermissions("customer:list")
    @ResponseBody
    public PageInfo<Customer> show(Integer pageNum, Integer pageSize, String keyword){
        List<Customer> datas=customerService.selPageCustomers(pageNum,pageSize,keyword);
        PageInfo<Customer> infos=new PageInfo<>(datas);
        if (infos.getSize()==0 && infos.getPageNum()>1){
            datas=customerService.selPageCustomers(pageNum-1,pageSize,keyword);
            infos=new PageInfo<>(datas);
        }
        return infos;
    }

    @RequestMapping("/delete")
    @RequiresPermissions("customer:delete")
    @ResponseBody
    public Result delete(Long id){
        int res= customerService.deleteById(id);
        Result result=new Result();
        if (1==res){
            result.setCode(1);
            result.setMsg("删除成功!");
        }else{
            result.setCode(0);
            result.setMsg("删除失败,请重试");
        }
        return result;
    }

    @RequestMapping("/deletes")
    @RequiresPermissions("customer:deletes")
    @ResponseBody
    public Result deletes(@RequestParam("ids[]") List<Long> ids){
        int res= customerService.deleteByIds(ids);
        Result result=new Result();
        if (1<=res){
            result.setCode(1);
            result.setMsg("删除成功!");
        }else{
            result.setCode(0);
            result.setMsg("删除失败,请重试");
        }
        return result;
    }

    @RequestMapping("/edit")
    @RequiresPermissions("customer:edit")
    public String customerEdit(Model m,Long customerId){
        if (null!=customerId){
            Customer customer = customerService.selById(customerId);
            m.addAttribute("customer",customer);
        }
        Subject subject= SecurityUtils.getSubject();
        User admin = (User) subject.getPrincipal();
        m.addAttribute("admin",admin);
        List<User> salesMans = userService.selUsersByRoleName(Constant.ROLE_SALESMAN);
        m.addAttribute("salesMans",salesMans);
        List<BaseData> areas=baseDataService.selBaseDataByParentName(Constant.BASE_DATA_INTERVAL);
        m.addAttribute("areas",areas);
        return "customerEdit";
    }

    @RequestMapping("/insert")
    @RequiresPermissions("customer:insert")
    @ResponseBody
    public Result insert(Customer customer){
        Result result=new Result(0,"新增客户失败,请重试！");
        int row=customerService.insertCustomer(customer);
        if (1==row){
            result.setCode(1);
            result.setMsg("新增客户成功！");
        }

        return result;
    }


    @RequestMapping("/update")
    @RequiresPermissions("customer:update")
    @ResponseBody
    public Result update(Customer customer){
        Result result=new Result(0,"客户信息修改失败,请重试！");
        int row=customerService.updateCustomer(customer);
        if (1==row){
            result.setCode(1);
            result.setMsg("客户信息修改成功！");
        }

        return result;
    }

}
